﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormProject.Diger
{
    public partial class Subelerfrm : Form
    {
        public Subelerfrm()
        {
            InitializeComponent();
        }
        DBBaglantiDataContext db = new DBBaglantiDataContext();
        int secilenil = 0;
        
        void formclear()
        {
            SubeAdi.Text = null;
            SehirID.SelectedIndex = -1;
            IlceID.SelectedIndex = -1;
            
        }
        
        private void Subelerfrm_Load(object sender, EventArgs e)
        {
            SehirID.DataSource = (from x in db.Sehirlers select x).ToList();
            SehirID.DisplayMember = "SehirAdi";
            SehirID.ValueMember = "SehirID";
            SehirID.SelectedIndexChanged += new EventHandler(SehirID_SelectedIndexChanged);
        }

        void SehirID_SelectedIndexChanged(object sender, EventArgs e)
        {
            secilenil =  Convert.ToInt32(SehirID.SelectedValue.ToString());           
            IlceID.DataSource = (from x in db.Ilcelers  where x.SehirID == int.Parse(SehirID.SelectedValue.ToString()) select x ).ToList();
            IlceID.DisplayMember = "IlceAdi";
            IlceID.ValueMember = "IlceID";
        }

        private void bt_kaydet_Click(object sender, EventArgs e)
        {
            
            Subeler sb= new Subeler();
            var KayitKontrol = (from x in db.Subelers where x.SubeAdi == SubeAdi.Text select x).FirstOrDefault();

            if (KayitKontrol != null)
            {
                KayitKontrol.SubeAdi = SubeAdi.Text;
                KayitKontrol.SubeSehirID = Convert.ToInt32(SehirID.SelectedValue);
                KayitKontrol.SubeIlceID = Convert.ToInt32(IlceID.SelectedValue);
                //sb.Cadde = Cadde.Text;
                //sb.Sokak = Sokak.Text;
                //sb.Semt = Semt.Text;
                //sb.PostaKodu = PostaKodu.Text;
                //sb.Telefon1 = Telefon1.Text;
                db.SubmitChanges();
                MessageBox.Show("Kayıt Güncellendi");
            }


            else
            {

                sb.SubeAdi = SubeAdi.Text;
                sb.SubeSehirID = Convert.ToInt32(SehirID.SelectedValue);
                sb.SubeIlceID = Convert.ToInt32(IlceID.SelectedValue);
                //sb.Cadde = Cadde.Text;
                //sb.Sokak = Sokak.Text;
                //sb.Semt = Semt.Text;
                //sb.PostaKodu = PostaKodu.Text;
                //sb.Telefon1 = Telefon1.Text;
                db.Subelers.InsertOnSubmit(sb);
                db.SubmitChanges();
                formclear();

            }
        }

        private void bt_sec_Click(object sender, EventArgs e)
        {
            SubelerList SubeListesi = new Diger.SubelerList();
            SubeListesi.AcanForm = this;
            SubeListesi.Show();

        }

        private void SubeAdi_Leave(object sender, EventArgs e)
        {
            SubeGetir();
            


        }

        public void SubeGetir()
        {
            try
            {
                var subegetir = (from x in db.Subelers join y in db.Sehirlers on x.SubeSehirID equals y.SehirID join z in db.Ilcelers on x.SubeIlceID equals z.IlceID where x.SubeAdi == SubeAdi.Text select new { x.SubeAdi,x.SubeSehirID,x.SubeIlceID 
                    //x.Cadde, x.Sokak, x.Semt, x.PostaKodu, x.Telefon1
                }).FirstOrDefault();

                SehirID.SelectedValue = subegetir.SubeSehirID;
                IlceID.SelectedValue = subegetir.SubeIlceID;
                //Cadde.Text = subegetir.Cadde;
                //Sokak.Text = subegetir.Sokak;
                //Semt.Text = subegetir.Semt;
                //PostaKodu.Text = subegetir.PostaKodu;
                //Telefon1.Text = subegetir.Telefon1;
            }
            catch (Exception)
            {

            }
        }

        private void SubeAdi_TextChanged(object sender, EventArgs e)
        {
            
        }

       
    }
}
